using System;

namespace Thunder.IrcClient
{
    public class MemberInfo : IComparable<MemberInfo>, IComparable<string>, IEquatable<MemberInfo>, IEquatable<string>
    {
        public string Name { get; set; }

        /// <summary>
        ///                     Compares the current object with another object of the same type.
        /// </summary>
        /// <returns>
        ///                     A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings: 
        ///                     Value 
        ///                     Meaning 
        ///                     Less than zero 
        ///                     This object is less than the <paramref name="other" /> parameter.
        ///                     Zero 
        ///                     This object is equal to <paramref name="other" />. 
        ///                     Greater than zero 
        ///                     This object is greater than <paramref name="other" />. 
        /// </returns>
        /// <param name="other">
        ///                     An object to compare with this object.
        ///                 </param>
        public int CompareTo(MemberInfo other)
        {
            if (this.Name.StartsWith("@") == true || other.Name.StartsWith("@") == true)
            {
                if (this.Name.StartsWith("@") == true && other.Name.StartsWith("@") == false)
                {
                    return -1;
                }

                if (this.Name.StartsWith("@") == false && other.Name.StartsWith("@") == true)
                {
                    return 1;
                }

                return this.Name.CompareTo(other.Name);
            }

            if (this.Name.StartsWith("+") == true || other.Name.StartsWith("+") == true)
            {
                if (this.Name.StartsWith("+") == true && other.Name.StartsWith("+") == false)
                {
                    return -1;
                }

                if (this.Name.StartsWith("+") == false && other.Name.StartsWith("+") == true)
                {
                    return 1;
                }

                return this.Name.CompareTo(other.Name);
            }

            return this.Name.CompareTo(other.Name);
        }

        /// <summary>
        ///                     Compares the current object with another object of the same type.
        /// </summary>
        /// <returns>
        ///                     A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings: 
        ///                     Value 
        ///                     Meaning 
        ///                     Less than zero 
        ///                     This object is less than the <paramref name="other" /> parameter.
        ///                     Zero 
        ///                     This object is equal to <paramref name="other" />. 
        ///                     Greater than zero 
        ///                     This object is greater than <paramref name="other" />. 
        /// </returns>
        /// <param name="other">
        ///                     An object to compare with this object.
        ///                 </param>
        public int CompareTo(string other)
        {
            return this.CompareTo(new MemberInfo {Name = other});
        }

        /// <summary>
        ///                     Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">
        ///                     An object to compare with this object.
        ///                 </param>
        public bool Equals(MemberInfo other)
        {
            return this.CompareTo(other) == 0;
        }

        /// <summary>
        ///                     Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">
        ///                     An object to compare with this object.
        ///                 </param>
        public bool Equals(string other)
        {
            return this.CompareTo(other) == 0;
        }

        public static implicit operator MemberInfo(string name)
        {
            return new MemberInfo {Name = name};
        }

        public override string ToString()
        {
            return this.Name;
        }
    }
}